home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / compress / tar321__.zip / SOURCES.ZIP / TAILOR.C < prev    next >
C/C++ Source or Header  |  1997-03-10  |  5KB  |  176 lines

  1. /* the file is to contain some system-specific code */
  2. #include "sysup.h"
  3. #include "modern.h"
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #ifdef MSDOS
  7. #    include <dos.h>
  8. #    include <string.h>
  9. #endif
  10. #ifndef __TURBOC__
  11. #    include <sys/types.h>
  12. #endif
  13. #include <sys/stat.h>
  14. #include "define.h"
  15.  
  16. #ifdef UNIX
  17. #    ifndef RMKDIR
  18. int bincall(name, args)
  19. char *name, *args;
  20. {
  21.    extern int fork(), execl(), wait();
  22.    int i; register k;
  23.    char b[24];
  24.  
  25.    if (fork() == 0) {
  26.       (void)execl(strcat(strcpy(b, "/bin/"),     name), name, args, 0);
  27.       (void)execl(strcat(strcpy(b, "/usr/bin/"), name), name, args, 0);
  28.       k = -1;
  29.    } else {
  30.       (void)wait(&i); k = i>>8;
  31.    }
  32.    return k;
  33. }
  34. #    endif
  35. #endif
  36.  
  37. #ifdef MSDOS
  38. int mapumode(umode) /* UNIX file mode to DOS attributes */
  39. {
  40.    register mask = 0;
  41.    if (0 == (umode & 00222)) mask |= FA_RDONLY;
  42.    if (0 == (umode & 00555)) mask |= FA_HIDDEN;
  43.    if (0 != (umode & 07000)) mask |= FA_SYSTEM;
  44.    return mask;
  45. }
  46.  
  47. int mapdattr(int dattr) /* DOS file attributes to UNIX mode */
  48. {
  49.    register mode = dattr & FA_LABEL ? S_IFLNK :
  50.                    dattr & FA_DIREC ? S_IFDIR : S_IFREG;
  51.    if (0 != (dattr & FA_SYSTEM)) mode |= 02000;
  52.    if (0 == (dattr & FA_HIDDEN)) mode |= 00444;
  53.    if (0 == (dattr & FA_RDONLY)) mode |= 00222;
  54.    return mode;
  55. }
  56.  
  57. static long ftime2unix(register unsigned short *dt)
  58. {
  59.    struct date d; struct time t;
  60.    d.da_year = (dt[1] >> 9) + 1980;
  61.    d.da_mon  = (dt[1] >> 5) & 15;
  62.    d.da_day  =  dt[1] & 31;
  63.    t.ti_hour =  dt[0] >> 11;
  64.    t.ti_min  = (dt[0] >> 5) & 63;
  65.    t.ti_sec  = (dt[0] & 31) << 1;
  66.    t.ti_hund = 0;
  67.    return dostounix(&d, &t);
  68. }
  69.  
  70. void fcb2stat(struct stat *s, unsigned char *ufcbP)
  71. {
  72.    s->st_dev = *ufcbP;
  73.    s->st_ino = *(short*)(ufcbP+27);
  74.    s->st_mode = mapdattr(*(ufcbP+12));
  75.    s->st_nlink = 1;
  76.    s->st_uid = s->st_gid = 0;
  77.    s->st_rdev = 0;
  78.    s->st_size = *(long*)(ufcbP + 29);
  79.    s->st_atime = s->st_mtime = s->st_ctime =
  80.       ftime2unix((unsigned short*)(ufcbP + 23));
  81. }
  82.  
  83. int _acces(char *path, int amode)
  84. {
  85.    register attr = lchmod(path, 0, 0);
  86.    if (attr == -1) return -1;
  87.    return (amode & ~7) ||
  88.       ((amode & 3) && (attr & (FA_HIDDEN+FA_LABEL))) ||
  89.       ((amode & 2) && (attr & (FA_SYSTEM+FA_RDONLY))) ?
  90.          (errno=EACCES, -1) : 0;
  91. }
  92.  
  93. int pwd(register char *dest)
  94. {
  95.    *dest++ = 'A' + dosgetdisk();
  96.    *dest++ = ':';
  97.    *dest++ = '\\';
  98.    return lgetcurdir(0, dest);
  99. }
  100.  
  101. int cwd(char *path)
  102. {
  103.    register k = hasdrive(path);
  104.    register char *p = path;
  105.    register char c = '\0';
  106.    if (k) {
  107.       if (dossetdisk(k-1)) return (errno=ENODEV, -1);
  108.       if ('\0' == *(p += 2)) return 0;
  109.    }
  110.    if ((k = strlen(p)-1) > 0 && (*(p += k) == '\\' || *p == '/')) {
  111.       c = *p; *p = '\0';
  112.    }
  113.    k = chdir(path);
  114.    if (c) *p = c;
  115.    return k;
  116. }
  117.  
  118. int hasdrive(register char *path)
  119. {
  120.    register c = LC(path[0]);
  121.    return c<'a' || c>'z' || path[1]!=':' ? 0 : c-('a'-1);
  122. }
  123.  
  124. int fnoffset(char *path)
  125. /* Calculate an offset to last filename element */
  126. {
  127.    register k;
  128.    register char *p = hasdrive(path) ? path+2 : path;
  129.    for (k=strlen(p); k && p[k-1]!='\\' && p[k-1]!='/';) --k;
  130.    return (int)(p-path) + k;
  131. }
  132.  
  133. int isarchive(s, p)
  134. register struct stat *s; register char *p;
  135. {
  136.    /* The algorithm below is not perfect: cluster numbers sometimes are
  137.     * not available (network, HPFS, NTFS) and name comparison gives no
  138.     * guarantee (remember substed drives).
  139.     * Some day we might want to redefine stat structure and use
  140.     * 64-bit drive and file IDs.
  141.     */
  142.    if (!isfile || sa.st_dev != s->st_dev) return FALSE;
  143.    /* Newly created archive have no any cluster, comparison useless */
  144.    if ((sa.st_ino ==  0 && s->st_ino != -1) ||
  145.        (sa.st_ino == -1 && s->st_ino == -1)) {
  146.       /* no real values for both archive and tested file */
  147.       if (archname) {
  148.          register i = fnoffset(archname);
  149.          if (!stricmp(archname+i, p+fnoffset(p))) {
  150.             /* file names are the same, compare drive:directory */
  151.             char tmp[PATHSIZE];
  152.             /* files are stored from current dir */
  153.             if (0 == pwd(tmp)) return !strnicmp(archname, tmp, i-1);
  154.          }
  155.       }
  156.       /* if comparison fails consider the files different */
  157.       return FALSE;
  158.    }
  159.    return sa.st_ino == s->st_ino;
  160. }
  161. #endif
  162.  
  163. long mtime(path)
  164. char *path;
  165. {
  166. #ifdef MSDOS
  167.    extern long dostounix();
  168.    unsigned long mtm = lgetmtime(path);
  169.    if (mtm != -1L) return ftime2unix((unsigned short*)&mtm);
  170. #else
  171.    struct stat st;
  172.    if (!stat(path, &st)) return st.st_mtime;
  173. #endif
  174.    return -1L; /* error */
  175. }
  176.